


var _sFetchXml =null;
var _sUrl = "";

var _i = 0;
var _sCustParams = "";
var _sIds = "";
var _bActionStarted = false;
var _a = window.dialogArguments;
var _oXml = CreateXmlDocument(false);
_oXml.onreadystatechange = wait;
var _sDialogXml = "";
var _xmlhttp = CreateXmlHttp();
var _xmlDoc = CreateXmlDocument(false);
var _sSubmitUrl = "";
var _iErrorCode = 0;
var _sErrorNumber = "";
var _sErrorDescription = "";
var _iErrorCount = 0;
var bPostActions = false;
var SELECTED = 1;
var ALLINQUERY = 2;

function ListActionJsWindowOnLoad()
{
if (!IsNull(crmGrid))
{
crmGrid.attachEvent("OnBeforeRefresh", RefreshMemberCount);
}
}

window.attachEvent('onload', ListActionJsWindowOnLoad);

function DisplayActionMsg(sMsg, iWidth, iHeight)
{
if (!IsNull(sMsg) || IsNull(_oActMsgPopup))
{

_oActMsgPopup = window.createPopup();

_oActMsgPopup.document.createStyleSheet(prependOrgName("/_common/styles/global.css.aspx?lcid=" + USER_LANGUAGE_CODE));
_oActMsgPopup.document.createStyleSheet(prependOrgName("/_common/styles/fonts.aspx?lcid=" + USER_LANGUAGE_CODE));

var oActMsg = _oActMsgPopup.document.createElement("DIV");
_oActMsgPopup.document.body.appendChild(oActMsg);

with(oActMsg)
{
className = "actionMsgBox";

with(style)
{
width = "100%";
height = "100%";
backgroundColor = "#ffffee";
border = "2px solid #000000";
}


innerHTML =  "<table width=100% height=100%><tr><td style='font: bold 15px;text-align: center; vertical-align: middle; cursor: wait; color: #000099'>" + sMsg + "<br><br></td></tr></table>";
}
}


_oActMsgPopup.show((window.document.body.clientWidth - iWidth)/2,
(window.document.body.clientHeight - iHeight)/2,
iWidth, iHeight, document.body);
}
function Go()
{

_iErrorCode = ERROR_NONE;
_iErrorCount = 0;
_sErrorNumber = "";
_sErrorDescription = "";

_bActionStarted = true;

window.setTimeout("performAction()", 0);
}


function wait()
{


if (_oXml.readyState == 4)
{
var iStatus = handleXMLErr(_oXml, true);

if (iStatus == ERROR_CONTINUE)
{
if (_iErrorCount == 0)
{

_iErrorCode = iStatus;

var errNode = _oXml.selectSingleNode("/error/code");
_sErrorNumber = (IsNull(errNode) ? "0x80004005" : errNode.text);


var descNode = _oXml.selectSingleNode("/error/description");
_sErrorDescription = (IsNull(descNode) ? "" : descNode.text);
}
_iErrorCount++;
}

if (_iErrorCode == ERROR_CONTINUE)
{
if (_iErrorCount == 1)
{
openErrorDlg(_sErrorNumber, _sErrorDescription);
}
else
{
alert(LOCID_LISTACTION_MULTIPLEERRORS);
}
}
else
{
window.returnValue = true;
}

window.close();
}

}


function performAction()
{
if (_sDialogXml == "")
{


_sDialogXml = "<node />";
}

_xmlDoc.loadXML(_sDialogXml);

_sSubmitUrl = prependOrgName("/MA/Lists/ListQualificationDlg/dlg_") + sAction + ".aspx?"  + _sCustParams;


_xmlhttp.open("POST", _sSubmitUrl, false);
SetTokenInHeader(_xmlhttp, _sSubmitUrl);
_xmlhttp.send(_xmlDoc);

_oXml.loadXML(_xmlhttp.responseXML.xml);
}


function cancel()
{
if (_bActionStarted)
{
if (window.confirm(formatString(LOCID_LISTACTION_CANCELCONFIRM, _a.length -_i)))
{
window.returnValue = true;
window.close();
}
}
else
{
window.close();
}
}

function RefreshMemberCount()
{
var memberCount = document.getElementById("MemberCount");
if (memberCount != null)
{
var command = new RemoteCommand("MarketingAutomation", "RetrieveList");
var sListId = this.parent.crmFormSubmit.crmFormSubmitId.value;
var columns = new Array();
columns.type = "string";
columns[0] = "membercount";

command.SetParameter("id", sListId);
command.SetParameter("columns", columns);

var oResult = command.Execute();
if (oResult.Success)
{
var resultXml = CreateXmlDocument(false);
resultXml.loadXML(oResult.ReturnValue);
var sMemberCount = CrmEncodeDecode.CrmHtmlEncode(resultXml.selectSingleNode("/list/membercount").text);
memberCount.innerHTML = sMemberCount;
}
}
}


function OpenManageMembersWizard(listMemberType, listId, iWidth, iHeight)
{

var url = prependOrgName("/MA/Lists/ListQualificationDlg/dlg_manage_members.aspx");
var result = openStdDlg(url, 'Search', iWidth, iHeight, true, false, "maximize:yes;minimize:yes");


switch(result)
{
case "0":

break;

case "1":

LaunchAddToListSimple(listId)
break;

case "2":

LaunchListQualification("lqAdd", listMemberType, listId);
break;

case "3":

LaunchListQualification("lqRemove", listMemberType, listId);
break;

case "4":

LaunchListQualification("lqKeep", listMemberType, listId);
break;
}
}


function LaunchListQualification(lqAction, listMemberType, listId)
{
var url = prependOrgName("/MA/Lists/ListQualificationDlg/dlg_query_build.aspx?");

url += "ListMemberType=" + listMemberType;
url += "&ListId=" + listId;
url += "&InvokeType=" + lqAction;
url += "&invoker=listmembers&restrictOtherEntities=1";

if(openStdDlg(url, 'Search', 820, 615, true, false, "maximize:yes;minimize:yes"))
{
crmGrid.Refresh()
}
}


function ProcessLQAdd(sFetchXml, sParams, iSelected, iOption)
{

if(iOption == SELECTED)
{

sParams += "&autoTrigger=1";
if(openStdDlg(prependOrgName("/_grid/cmds/dlg_addtolist.aspx?") + sParams, iSelected, 400, 200))
{
window.returnValue = true;
}
else
{
window.returnValue = false;
}

window.close();
}

else if(iOption == ALLINQUERY)
{
if(sFetchXml != null)
{
_sFetchXml = sFetchXml;
}

_sCustParams = sParams + "&iOption=" + iOption + "&iViewType=" + crmGrid.GetParameter("viewtype");
_iErrorCode = ERROR_NONE;
_iErrorCount = 0;
_sErrorNumber = "";

_bActionStarted = true;
_sUrl = prependOrgName("/MA/Lists/ListQualificationdlg/dlg_List_add.aspx?");
DisplayActionMsg(LOCID_BI_PROCESSING, 350, 150);
window.setTimeout("postBackFetch()", 50);

window.returnValue = true;
}

else
{
window.returnValue = false;
}
}


function ProcessLQRemove(sFetchXml, sParams, iSelected, iOption)
{
if(iOption == SELECTED || iOption == ALLINQUERY)
{
if(sFetchXml != null)
{
_sFetchXml = sFetchXml;
}


if(iOption == SELECTED)
{
_sIds = iSelected;
}

_sCustParams = sParams + "&iOption=" + iOption + "&iViewType=" + crmGrid.GetParameter("viewtype");
_iErrorCode = ERROR_NONE;
_iErrorCount = 0;
_sErrorNumber = "";

_bActionStarted = true;
_sUrl = prependOrgName("/MA/Lists/ListQualificationdlg/dlg_List_qualify.aspx?");

DisplayActionMsg(LOCID_BI_PROCESSING, 350, 150);
window.setTimeout("postBackFetch()", 50);

window.returnValue = true;
}

else
{
window.returnValue = false;
}
}


function ProcessLQKeep(sFetchXml, sParams, iSelected, iOption)
{

ProcessLQRemove(sFetchXml, sParams, iSelected, iOption)
}

function postBackFetch()
{
if(_sFetchXml == "" || _sFetchXml == null)
{

_sDialogXml = "<node/>";
}
else
{
_sDialogXml = _sFetchXml;
}

_sDialogXml = "<root>" + _sDialogXml + "<Ids>";


if(_sIds != null && _sIds.length != 0)
{
var iLen = _sIds.length;

for(var i=0; i<iLen; i++)
{
_sDialogXml = _sDialogXml + "<Id>" + _sIds[i] + "</Id>";
}
}

_sDialogXml = _sDialogXml + "</Ids></root>";
_xmlDoc.loadXML(_sDialogXml);
_sSubmitUrl = _sUrl + _sCustParams;
_xmlhttp.open("POST", _sSubmitUrl, false);
SetTokenInHeader(_xmlhttp, _sSubmitUrl);
_xmlhttp.send(_xmlDoc);
_oXml.loadXML(_xmlhttp.responseXML.xml);
}

function GetListMemberType()
{
var command = new RemoteCommand("MarketingAutomation", "RetrieveList");
var sListId = this.parent.crmFormSubmit.crmFormSubmitId.value;
var columns = new Array();
columns.type = "string";
columns[0] = "createdfromcode";

command.SetParameter("id", sListId);
command.SetParameter("columns", columns);

var oResult = command.Execute();
if (oResult.Success)
{
var resultXml = CreateXmlDocument(false);
resultXml.loadXML(oResult.ReturnValue);
return (resultXml.selectSingleNode("/list/createdfromcode").text);
}
return "";
}


function LaunchAddToListSimple(listId)
{
var qs = new QueryString();
var a = new Array();
var sMemberType = GetListMemberType();
var itemObjectId = "";
var itemObjectTypeCode = "";
var bMakeCall = false;
var lookupItems;

if (Lead == sMemberType)
{
lookupItems = LookupObjects(null, "multi","openlead", sMemberType,0,null, null, "1");
}
else
{
lookupItems = LookupObjects(null, "multi","activityrelatedcustomer", sMemberType,0,null, null, "1");
}


if ( lookupItems )
{
if ( lookupItems.items.length > 0 )
{
for (i=0; i<lookupItems.items.length; i++)
{
a[i]= lookupItems.items[i].id;
}
bMakeCall = true;
}
}
if(bMakeCall)
{
oResult = openStdDlg(prependOrgName("/_grid/cmds/dlg_addtolist.aspx?autoTrigger=1&iObjType=") + sMemberType + "&iTotal=" + a.length + "&sIds=" + ""+ "&itemObjectId=" + listId + "&itemObjectTypeCode=" +"", a, 400, 200 );
if (oResult)
{
crmGrid.Refresh();
}
}
}
